﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FAdvWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FAdvWP";
            }
        }


        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FAdvWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("Title", "SoYT-Ảnh quảng cáo");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                        new ViewCls("AdvBanner",GetText("Banner","Ảnh quảng giữa trang"),0),
                    new ViewCls("Right",GetText("Right","Album ảnh bên phải"),1),              
                    new ViewCls("RightLink",GetText("RightLink","Ảnh liên kết bên phải"),2),   
                };
            }
        }

        private string ClientRenderWebPartRightImg(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);

            StringBuilder Html = new StringBuilder();
            Html.Append("<div class=\"rightcolStyle\">");

            for (int iIndex = 0; iIndex < dtItems.Rows.Count; iIndex++)
            {
                string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                string Url = "";
                try
                {
                    Url = (string)dtItems.Rows[iIndex]["Url"];
                }
                catch { Url = "#"; }
                Html.Append("<a href=\"" + Url + "\"><img class = \"linkimg\" src=\"" + ImageUrl + "\" /></a>");
            }
            Html.Append("</div>" +
                    "<div class=clrfix></div>");

            return Html.ToString();
        }


        private string ClientRenderWebPartAdvBanner(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);

            string Html = "";

            string AdvImageItemId = (string)dtItems.Rows[0]["AdvImageItemId"];
            string FileImage = (string)dtItems.Rows[0]["FileImage"];
            string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
            string Url = "";
            try
            {
                Url = (string)dtItems.Rows[0]["Url"];
            }
            catch { Url = "#"; }
            Html +=
            "<a class=\"adv1\" href=\"#\"><img src=\"img/adv1.jpg\" /></a>\n";
            return Html;
        }


        private string ClientRenderWebPartRight(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            string AdvImageId = TabModuleCls.Id;
            string belongSite = TabModuleCls.belongSite;
            try
            {
                StringBuilder Html = new StringBuilder();
                //DataTable dtItems = new FAlbumBLL().ReadingItems(TabModuleCls.Id);    

                DataTable dtItems = new FAlbumBLL().ReadingAlbumDefaulView();

                int ICount = 0;
                if (dtItems.Rows.Count > 5)
                {
                    ICount = 5;
                }
                else
                {
                    ICount = dtItems.Rows.Count;
                }
                Html.Append(
                   "<div id=\"album\">" +
                    "<div id=\"slider2\" class=\"sliderwrapper\">");

                StringBuilder SubHtml = new StringBuilder();
                SubHtml.Append(
                    "<div class=\"pagination\" id=\"paginate-slider2\">" +
                    "<ul>");
                for (int iIndex = 0; iIndex < ICount; iIndex++)
                {
                    string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                    string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                    string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                    string Url = "";
                    try
                    {
                        Url = Ultility.GetAlbumDetails(belongSite, TabModuleCls.Title, AdvImageId.Substring(0, 8), "picture");
                    }
                    catch { Url = "#"; }

                    Html.Append(
                        "<div class=\"contentdiv\">" +
                        "<img src=\"" + ImageUrl + "\">" +
                        "</div>");
                    SubHtml.Append(
                            "<li>" +
                                "<a class=\"toc someotheclass\" href=\"" + Url + "\" rel=" + (iIndex + 1) + ">" +
                                    "<img src=" + ImageUrl + " />" +
                                    "<div class=\"clrfix\"></div>" +
                                "</a>" +
                            "</li>"
                        );
                }
                SubHtml.Append("</ul></div>");
                Html.Append("</div>");
                Html.Append(
                    SubHtml.ToString() +
                    "</div>" +
                  "<script type=\"text/javascript\">\n" +
                    "					featuredcontentslider.init({\n" +
                    "						id:\"slider2\", //id of main slider DIV\n" +
                    "						contentsource: [\"inline\", \"\"], //Valid values: [\"inline\", \"\"] or [\"ajax\", \"path_to_file\"]\n" +
                    "						toc:\"markup\", //Valid values: \"#increment\", \"markup\", [\"label1\", \"label2\", etc]\n" +
                    "						nextprev:[\"prev\", \"Next\"], //labels for \"prev\" and \"next\" links. Set to \"\" to hide.\n" +
                    "						revealtype:\"mouseover\", //Behavior of pagination links to reveal the slides: \"click\" or \"mouseover\"\n" +
                    "						enablefade:[true, 0.1], //[true/false, fadedegree]\n" +
                    "						autorotate:[true, 3000], //[true/false, pausetime]\n" +
                    "						onChange:function(previndex, curindex){ //event handler fired whenever script changes slide\n" +
                    "						//previndex holds index of last slide viewed b4 current (1=1st slide, 2nd=2nd etc)\n" +
                    "						//curindex holds index of currently shown slide (1=1st slide, 2nd=2nd etc)\n" +
                    "						}\n" +
                    "						}) \n" +
                    "					\n" +
                    "					</script>\n"
                    );

                return Html.ToString();
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }


        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("AdvBanner"))
                {
                    return ClientRenderWebPartAdvBanner(TabModuleId, HasPorlet);
                }

                if (ViewName.Equals("Right"))
                {
                    return ClientRenderWebPartRight(TabModuleId, HasPorlet);
                }
                if (ViewName.Equals("RightLink"))
                {
                    return ClientRenderWebPartRightImg(TabModuleId, HasPorlet);
                }
                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }



        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            string Query = "select Id=AdvImageId,Subject=[" + ActiveLanguage + "Subject] from TableAdvImage where belongSite='" + belongSite + "'";
            SqlConnection sqlConnection = new SqlConnection(Environments.ConnectionString);
            sqlConnection.Open();
            SqlCommand sqlCommand = sqlConnection.CreateCommand();
            sqlCommand.CommandText = Query;
            DataTable dtResult = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sqlCommand);
            da.Fill(dtResult);
            return dtResult;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.FAlbumWP), ActivePage);
        }
    }
}
